package com.lyj.friend.service.user.serviceImpl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.lyj.api.domain.dto.JudgeSubmitDTO;
import com.lyj.api.domain.vo.UserQuestionResultVO;
import com.lyj.common.core.constants.Constants;
import com.lyj.common.core.domain.R;
import com.lyj.common.core.enums.ProgramType;
import com.lyj.common.core.enums.ResultCode;
import com.lyj.common.core.utils.ThreadLocalUtil;
import com.lyj.friend.domain.question.Question;
import com.lyj.friend.domain.question.QuestionCase;
import com.lyj.friend.domain.question.es.QuestionES;
import com.lyj.friend.domain.user.dto.UserSubmitDTO;
import com.lyj.friend.service.user.UserQuestionService;
import com.lyj.security.exception.ServiceException;
import lombok.val;
import org.springframework.stereotype.Service;
import com.lyj.api.RemoteJudgeService;
import com.lyj.friend.elasticsearch.QuestionRepository;
import com.lyj.friend.mapper.question.QuestionMapper;
import com.lyj.friend.mapper.user.UserSubmitMapper;
import com.lyj.friend.rabbit.JudgeProducer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LIUYANJIAO
 * Date: 2025-10-31
 * Time: 17:36
 */
@Service
public class UserQuestionServiceImpl implements UserQuestionService {
    @Autowired
    private QuestionRepository questionRepository;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private UserSubmitMapper userSubmitMapper;

    @Autowired
    private RemoteJudgeService remoteJudgeService;

    @Autowired
    private JudgeProducer judgeProducer;
    @Override
    public R<UserQuestionResultVO> submit(UserSubmitDTO submitDTO) {
        Integer programType = submitDTO.getProgramType();
        if(ProgramType.JAVA.getValue().equals(programType)){
            //按照java逻辑进行处理
            //获得拼接完整的题目信息
            JudgeSubmitDTO judgeSubmitDTO = assembleJudgeSubmitDTO(submitDTO);
            //返回容器执行结果
            return remoteJudgeService.doJudgeJavaCode(judgeSubmitDTO);
        }
        //返回不支持该功能错误
        throw new ServiceException(ResultCode.FAILED_NOT_SUPPORT_PROGRAM);
    }

    @Override
    public int rabbitSubmit(UserSubmitDTO submitDTO) {
        return 0;
    }

    @Override
    public UserQuestionResultVO exeResult(Long examId, Long questionId, String currentTime) {
        return null;
    }

    private JudgeSubmitDTO assembleJudgeSubmitDTO(UserSubmitDTO submitDTO) {
        Long questionId = submitDTO.getQuestionId();
        //获得题目详细信息
        QuestionES questionES = questionRepository.findById(questionId).orElse(null);
        //将输入和已知题目信息拼接成完整 提交后的信息
        JudgeSubmitDTO judgeSubmitDTO = new JudgeSubmitDTO();
        if(questionES != null){
            BeanUtil.copyProperties(questionES,judgeSubmitDTO);
        }else {
            //从数据库中查找，存放在es中
            Question question = questionMapper.selectById(questionId);
            BeanUtil.copyProperties(question,judgeSubmitDTO);
            questionES = new QuestionES();
            BeanUtil.copyProperties(question,questionES);
            questionRepository.save(questionES);
        }
        //将输入的信息同步到judgeSubmitDTO中
        judgeSubmitDTO.setUserId(ThreadLocalUtil.get(Constants.USER_ID, Long.class));
        judgeSubmitDTO.setExamId(submitDTO.getExamId());
        judgeSubmitDTO.setProgramType(submitDTO.getProgramType());
        judgeSubmitDTO.setUserCode(submitDTO.getUserCode());
        //测试用例列表
        List<QuestionCase> questionCaseList = JSONUtil.toList(questionES.getQuestionCase(), QuestionCase.class);
        List<String> inputList = questionCaseList.stream().map(QuestionCase::getInput).toList();
        judgeSubmitDTO.setInputList(inputList);
        List<String> outputList = questionCaseList.stream().map(QuestionCase::getOutput).toList();
        judgeSubmitDTO.setOutputList(outputList);
        return judgeSubmitDTO;
    }
}
